package reflecttest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface Dog {
    void info();

    void run();
}

class GunDog implements Dog {

    @Override
    public void info() {
        System.out.println("I am is GunDog");
    }

    @Override
    public void run() {
        System.out.println("I can running very fast");
    }
}

class DogUtil {
    public void method1() {
        System.out.println("method1");
    }

    public void method2() {
        System.out.println("method2");
    }
}

class MyInvokationHandler2 implements InvocationHandler {
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        DogUtil dogUtil = new DogUtil();
        dogUtil.method1();
        Object result = method.invoke(target, args);
        dogUtil.method2();
        return null;
    }
}

class MyProxyFactory {
    public static Object getProxy(Object target) {
        MyInvokationHandler2 handler = new MyInvokationHandler2();
        handler.setTarget(target);
        Object object = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), handler);
        return object;
    }
}

public class ProxyAopTest {
    public static void main(String[] args) {
        Dog target = new GunDog();
        Dog dog = (Dog) MyProxyFactory.getProxy(target);
        dog.info();
        dog.run();
    }
}
